package com.yanqu.road.server.cmd.activity.chinesechess;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.pb.activity.ChineseChessProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.LinkedList;

@Cmd(code = Protocol.C2_CHINESECHESS_ACTIVITY_GAME_OVER, desc = "")
public class CrossChineseChessGaneOverCmd extends CrossCommand {

    private int activityId;
    private int conditionType;
    private long userId;
    private int gameNo;
    private long lastScore;//活动结束后上次排名，没有入榜单是-1
    private long nowScore;//活动结束后本次排名，没有入榜单是-1
    private long topScore;//最高排名
    private boolean isPractice;//是不是练习模式
    LinkedList<CrossUserRank> otherRankList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChineseChessProto.CrossChineseChessGetRankChangeReqMsg reqMsg = ChineseChessProto.CrossChineseChessGetRankChangeReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        gameNo = reqMsg.getGameNo();
        isPractice = reqMsg.getIsPractice();
        lastScore = reqMsg.getLastScore();
        nowScore = reqMsg.getNowScore();
        topScore = nowScore;
        if(lastScore > nowScore){
            topScore = lastScore;
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityRankMgr.changeActivityNewRank(activityId, conditionType,null);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, conditionType);
        int newRank = -1;
        if (rankListModel != null) {
            otherRankList = rankListModel.getRankList();
            CrossUserRank otherUserRank = new CrossUserRank();
            otherUserRank.setType(activityId);
            otherUserRank.setUserId(userId);
            otherUserRank.setBigRankType(conditionType);
            otherUserRank.setValue(BigInteger.valueOf(topScore));
            otherUserRank.setParam("");
            rankListModel.otherRankChange(otherUserRank,otherRankList);
            newRank = rankListModel.getOtherRank(userId,serverId,otherRankList);
        }
        ChineseChessProto.CrossChineseChessGetRankChangeRespMsg.Builder msg = ChineseChessProto.CrossChineseChessGetRankChangeRespMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setGameNo(gameNo);
        msg.setLastRank(0);
        msg.setNowRank(0);
        msg.setTopRank(newRank);
        msg.setIsPractice(isPractice);
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_CHINESECHESS_RANK_CHANGE, msg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    private void rankChange(){
        //初始化一下，不然没有
        ActivityRankMgr.changeActivityNewRank(activityId, conditionType,null);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, conditionType);
        int oldRank = -1;
        int newRank = -1;
        if (rankListModel != null) {
            oldRank = rankListModel.getMyRank(userId, serverId);
            otherRankList = rankListModel.getRankList();
            CrossUserRank otherUserRank = new CrossUserRank();
            otherUserRank.setType(activityId);
            otherUserRank.setUserId(userId);
            otherUserRank.setBigRankType(conditionType);
            otherUserRank.setValue(BigInteger.valueOf(nowScore));
            otherUserRank.setParam("");
            rankListModel.otherRankChange(otherUserRank,otherRankList);
            newRank = rankListModel.getOtherRank(userId,serverId,otherRankList);
        }
        ChineseChessProto.CrossChineseChessGetRankChangeRespMsg.Builder msg = ChineseChessProto.CrossChineseChessGetRankChangeRespMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setGameNo(gameNo);
        msg.setLastRank(oldRank);
        msg.setNowRank(newRank);
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_CHINESECHESS_RANK_CHANGE, msg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
